<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
        <el-form-item label="等级" prop="level">
          <el-select v-model="queryParams.level" placeholder="请选择等级" clearable>
            <el-option
              v-for="dict in salay_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="mt20">

      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="6"><span style="font-size: large">绩效等级设置</span></el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['wms:PsalarySetup:add']"
          >新增
          </el-button>
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['wms:PsalarySetup:export']"
          >导出
          </el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="PsalarySetupList" border class="mt20">
        <el-table-column label="等级" prop="level">
          <template #default="scope">
            <dict-tag :options="salay_level" :value="scope.row.level"/>
          </template>
        </el-table-column>
        <el-table-column label="基础工资" prop="baseSalary"/>
        <el-table-column label="备注" prop="remarks"/>
        <el-table-column label="操作" align="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['wms:PsalarySetup:edit']">修改
            </el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['wms:PsalarySetup:remove']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-row>
        <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-row>

    </el-card>
    <!-- 添加或修改绩效等级设置对话框 -->
    <el-drawer :title="title" v-model="open" size="50%" append-to-body>
      <el-form ref="PsalarySetupRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="等级" prop="level">
          <el-select v-model="form.level" placeholder="请选择等级">
            <el-option
              v-for="dict in salay_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="基础工资" prop="baseSalary">
          <el-input v-model="form.baseSalary" placeholder="请输入基础工资"/>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="PsalarySetup">
import {
  listPsalarySetup,
  getPsalarySetup,
  delPsalarySetup,
  addPsalarySetup,
  updatePsalarySetup
} from "@/api/wms/PsalarySetup";

const {proxy} = getCurrentInstance();
const {salay_level} = proxy.useDict('salay_level');

const PsalarySetupList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    level: undefined,
  },
  rules: {
    level: [
      {required: true, message: "等级不能为空", trigger: "change"}
    ],
    baseSalary: [
      {required: true, message: "基础工资不能为空", trigger: "blur"},
      {
        //必须是数字
        pattern: /^[0-9]+\.?[0-9]*$/,
        message: "请输入数字",
        trigger: "blur"
      }
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询绩效等级设置列表 */
function getList() {
  loading.value = true;
  listPsalarySetup(queryParams.value).then(response => {
    PsalarySetupList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    level: null,
    baseSalary: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remarks: null
  };
  proxy.resetForm("PsalarySetupRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加绩效等级设置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getPsalarySetup(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改绩效等级设置";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["PsalarySetupRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updatePsalarySetup(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addPsalarySetup(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除绩效等级设置编号为"' + _ids + '"的数据项？').then(function () {
    loading.value = true;
    return delPsalarySetup(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/PsalarySetup/export', {
    ...queryParams.value
  }, `PsalarySetup_${new Date().getTime()}.xlsx`)
}

getList();
</script>
